﻿var  _gPageSize=5;
var  _mPager;
var  Rank=5;

$(document).ready(_pagerInit);
/*********************分页控件初始化**************************/
function _pagerInit()
{
     _mPager=new pager();  //初始化  
     _mPager.functionName="getPagingCommentList";
     _mPager.pageSize=_gPageSize;
     _mPager.buttonText='>>';
     _mPager.pagerContainerId="LabPager"; //把分页控件放到一个容器里面
     getPagingCommentList(1);
}


/*********************分页获取评论*****************************/

function getPagingCommentList(_pageIndex)
{  
    if(arguments.length==0)
	{
		_pageIndex=_mPager.getInputPageIndex();
	}
    _mPager.pageIndex=_pageIndex;
    var goodsId = document.getElementById("hidGoodsId").value;
  
    $.ajax
	(
		{
		        
		     type:"POST",
             url:"../Handler/UserComment.ashx",
	           data:"PageIndex=" + _pageIndex + "&PageSize=" + _gPageSize + "&goodsId="
	         	   +goodsId,
	         success:function(val_comment)
			 {
			 
			    if(val_comment.indexOf("[")==0)
				{
				    //alert(val_comment);
				    var commentList=eval(val_comment);
                    var recordCount=parseInt(commentList[0].RecordCount);
                    _mPager.recordCount=recordCount;
				    _mPager.setPagingHtml();
			        getUserCommentList(commentList);		 
				}	
			},
			error:function()
			{
			    //alert("错误");
			    document.getElementById("tbCommentList2").style.display = "none";
			} 		 		
		}
	);	
}

/****
**显示评论
****/
function  getUserCommentList(commentList)
{
	 var list='';
	 var i = 1; 
	 var temp = ""; var worth = "";var num="";
	 while(i < commentList.length)
	 {
	    list+="<div class=\"comTit\">";
	    list+="<span>"+commentList[i].UserName+"</span><label>"+commentList[i].AddTime+"</label>";
	    list+="</div>";
	    list+="<strong>标题:"+commentList[i].Title+"</strong>";
        switch (parseInt(commentList[i].Rank))
        {
            case 5:
                temp = "../images/0001.gif";
                worth = "讨厌";
                num="一星";
                break;
            case 4:
                temp = "../images/0002.gif";
                worth = "不喜欢";
                num="二星";
                break;
            case 3:
                temp = "../images/0003.gif";
                worth = "一般";
                num="三星";
                break;
            case 2:
                temp = "../images/0004.gif";
                worth = "喜欢";
                num="四星";
                break;
            case 1:
                temp = "../images/0005.gif";
                worth = "非常喜欢";
                num="五星";
                break;
        }
        debugger;
        var commnent=ChangeRow(commentList[i].CommentContent,100);
	    list+="<div class=\"comStar\">商品评分：<img src="+temp+" alt="+num+" />"+worth+"</div>";
	    list+="<div>评价:<div>"+commnent+"</div></div>";
        i+=1;
	 }
	  
	 $("#tbCommentList2").html(list);  
}

function ChangeRow(str,len)
{
 //手动换行,len个字符长度为1行
var newStr="";
var arr=new Array();
var n=str.length/len+1;
var l=str.length%len;
for(var i=1;i<=Math.floor(n);i++)
{
    if(i==n)
    {
        arr.push(str.subString((i-1)*len,l));
    }
    else
    {
        var s=str.substring((i-1)*len,i*len);
        arr.push(s);
        arr.push("<br>");
    }
}
for(var i=0;i<arr.length;i++)
{
    newStr=newStr+arr[i].toString();
}
return newStr;
}

/*********************
*显示评论级别
**********************/
function showCommentGrade(number)
{
    Rank=number;
    var temp=parseInt(number,10);
    for(var i=5;i>=1;i--)
    {
       var sel_id_old=document.getElementById("img_"+i);
       if(sel_id_old)
       {
             sel_id_old.src="../images/star_gray_big.gif";
       }
    } 
    for( var j=5;j>=number;j--)
    {
        var sel_id=document.getElementById("img_"+j);
        if(sel_id)
        {
            sel_id.src="../images/star_red_big.gif";
        }
    }
    
  var _info;
     switch(temp){
        case 5:
            _info="讨厌";
            break;
        case 4:
            _info="不喜欢";
            break;
        case 3:
            _info="一般";
            break;
        case 2:
            _info="喜欢";
            break;
        case 1:
            _info="非常喜欢";
            break;
    }
      var lbl=document.getElementById("lbl");
      if(lbl)
      {
           lbl.innerHTML=_info; 
      }
}

/*********************
*用户提交评论
************************/
function submitComment(goodsId)
{
    var CommentContent = $.trim($("#comment").val());
    var Title=$.trim($("#title").val());
    if(CommentContent == ""||CommentContent=="提示:如果您没有购买过此商品是不能够评论的,但是可以登录留言!")
    {
        alert("请输入评论内容!");
        return;
    }
    if(CommentContent.length<3)
    {
        alert("评论内容太短!");
        return;
    }
    if(CommentContent.length>1999)
    {
        alert("评论内容太长");
        return;
    }
    if(Title==""||Title=="请输入评论标题")
    {
        alert("请输入评论标题!");
        return;    
    }
    if(Title.length>127)
    {
         alert("评论标题太长!");
         return;
    }
    if(Rank<1)
    {
        alert("请选择评分!");
        return;
    }
        
      $.ajax
      (
        {
            type:"POST",
            url:"../Handler/SubitUserComment.ashx",
            data:"CommentContent="+escape(CommentContent)+"&Title="+escape(Title)+"&goodsId="+goodsId+"&rank="+Rank, 
            success: function(ret)
            {
                if(ret  == "000000")
                {
                    alert("评论成功!");
                    document.getElementById("comment").value="";
                    document.getElementById("title").value="";
                    getPagingCommentList(1);
                }
                else
                {
                    alert(ret);
                }
            },
            error: function()
            {
                alert("评论失败!");
            }
        }
      );
      
}
/*********************
*清空评论标题和内容
************************/
function  checkClear()
{
  document.getElementById("comment").value="";
  document.getElementById("title").value="";  
  getPagingCommentList(1);
}